<?php
class Pageley_Log_Logger {

	private $appenders;
	private $sourceClass;
	private $currentLevel;

	public function __construct($sourceClass, $currentLevel, $appenders) {
		$this->appenders = $appenders;
		$this->currentLevel = $currentLevel;
		$this->sourceClass = $sourceClass;
	}

	public function log($level, $message) {
		if($this->isLevelEnabled($level)) {
			$timestamp = new Pageley_Util_Timestamp();
			$date = $timestamp->format("d-m-Y H:i:s");
            foreach($this->appenders as $appender) {
    			$appender->append(
    				"[" . $date . "] [" .
    				$level . "] [" .
    				$this->sourceClass . "] " .
    				$message, $level
    			);
            }
		}
	}

	public function trace($message) {
		$this->log(Pageley_Log_Level::$TRACE, $message);
	}

	public function debug($message) {
		$this->log(Pageley_Log_Level::$DEBUG, $message);
	}

	public function info($message) {
		$this->log(Pageley_Log_Level::$INFO, $message);
	}

	public function warn($message) {
		$this->log(Pageley_Log_Level::$WARN, $message);
	}

	public function error($message) {
		$this->log(Pageley_Log_Level::$ERROR, $message);
	}

	public function fatal($message) {
		$this->log(Pageley_Log_Level::$FATAL, $message);
	}

	public function isLevelEnabled($level) {
		return $level->compareTo($this->currentLevel) >= 0;
	}

	public function isTraceEnabled() {
		return $this->isLevelEnabled(Pageley_Log_Level::$TRACE);
	}

	public function isDebugEnabled() {
		return $this->isLevelEnabled(Pageley_Log_Level::$DEBUG);
	}

	public function isInfoEnabled() {
		return $this->isLevelEnabled(Pageley_Log_Level::$INFO);
	}

	public function getSourceClass() {
		return $this->sourceClass;
	}

	public function getCurrentLevel() {
		return $this->currentLevel;
	}
}